<template>
  <div>
    <div>
      <el-form :inline="true" :model="selectType" ref="selectType" class="demo-form-inline">
        <el-row :gutter="5">
          <el-col :span="6">
            <el-form-item prop="typeName" label="商品名称:" style="width:100%" label-width="90px">
              <el-input v-model="selectType.typeName" placeholder="商品名称" size="medium"
                        autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="createDate" label="创建时间:" style="width:100%" label-width="90px">
              <el-date-picker
                  v-model="selectType.createDate"
                  type="daterange"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :default-time="['00:00:00', '23:59:59']" style="width:215px" size="medium" autocomplete="off"
                  clearable>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right">
            <el-form-item>
              <el-button type="primary" @click="onSubmit('selectType')" size="medium">查询</el-button>
              <el-button @click="resetForm('selectType')" size="medium">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-card style="width: 100%;  height: calc(100vh - 325px);">
        <el-row :gutter="25">
          <el-col :span="21">
            <el-popover
                placement="right"
                width="200"
                trigger="click" v-model="showPopover" @hide="handleHide">
              <el-form :model="showCol" status-icon ref="columnValues" class="demo-ruleForm">
                <el-form-item>
                  <el-checkbox-group v-model="columnValues" @change="handleCheckedCitiesChange">
                    <el-row :gutter="20">
                      <el-col :span="12" v-for="col in columns">
                        <el-checkbox :label="col" :key="col">{{ col }}</el-checkbox>
                      </el-col>
                    </el-row>
                  </el-checkbox-group>
                </el-form-item>
                <el-form-item style="text-align: right">
                  <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange"
                               style="margin-right: 20px">全选
                  </el-checkbox>
                  <el-button type="primary" @click="submitFormShowCol('showCol')" size="mini">应用</el-button>
                  <el-button @click="resetFormShowCol('showCol')" size="mini">重置</el-button>
                </el-form-item>
              </el-form>
              <el-button slot="reference" type="primary" plain size="small" icon="el-icon-s-tools">自定义列</el-button>
            </el-popover>
          </el-col>
          <el-col :span="2" >
            <el-button type="primary" icon="el-icon-plus" size="small">添加商品类型</el-button>
          </el-col>
          <el-col :span="1" ></el-col>
        </el-row>
        <template>
          <el-table :data="tableData" max-height="270" v-loading="loading" element-loading-text="拼命加载中"
                    element-loading-spinner="el-icon-loading">
            <template slot="empty">
              <el-empty :image-size="100" description='暂无数据'></el-empty>
            </template>
            <el-table-column fixed prop="id" label="编号" sortable></el-table-column>
            <el-table-column fixed prop="typeName" v-if="showCol.includes('商品类型名称')" label="商品类型名称"
                             width="200" sortable></el-table-column>
            <el-table-column prop="createBy" v-if="showCol.includes('创建人')" label="创建人" width="150"></el-table-column>
            <el-table-column prop="createDate" v-if="showCol.includes('创建时间')" label="创建时间" width="180"
                             sortable></el-table-column>
            <el-table-column prop="lastUpdateBy" v-if="showCol.includes('修改人')" label="修改人"
                             width="150"></el-table-column>
            <el-table-column prop="lastUpdateDate" v-if="showCol.includes('修改时间')" label="修改时间" width="180"
                             sortable></el-table-column>
            <el-table-column fixed="right" label="操作" width="120">
              <template slot-scope="scope">
                <el-button
                    @click="update(scope.$index, tableData)"
                    type="text"
                    size="small">
                  编辑
                </el-button>
                <el-button
                    @click="delete(scope.$index, tableData)"
                    type="text"
                    size="small" style="color:red;">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <div class="block" style="margin-top: 15px;text-align: right">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[10,20, 50, 100, 200,500]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total" background small>
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectType: {
        typeName: "",
        createDate: []
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      loading: false,
      selectTypeValue: "",
      typeList: [],
      showCol: ['商品类型名称', '创建人', '创建时间', '修改人', '修改时间'],
      columnValues: ['商品类型名称', '创建人', '创建时间', '修改人', '修改时间'],
      columns: ['商品类型名称', '创建人', '创建时间', '修改人', '修改时间'],
      checkAll: true,
      isIndeterminate: true,
      showPopover: false
    }
  },
  methods: {
    handleClose(done) {
      if (this.digloading) {
        return;
      }
      this.cancelForm();
    },
    insertSubmitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.insert();
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.digloading = false;
      this.dialog = false;
      this.insertData = {productCommodityCode: "", productTitle: "", purchasePrice: 0.0, retailPrice: 0.0, typeId: ""};
      this.insertDataDisabled = {
        productTitleDisabled: false,
        purchasePriceDisabled: false,
        retailPriceDisabled: false,
        typeIdDisabled: false
      };
      clearTimeout(this.timer);
    },
    handleCheckAllChange(val) {
      this.columnValues = val ? this.columns : [];
      this.isIndeterminate = false;
    }, handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.columns.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.columns.length;
    },
    submitFormShowCol(formName) {
      this.showCol = this.columnValues;
      this.showPopover = false;
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
      }, 1000);
    },
    resetFormShowCol(formName) {
      this.columnValues = ['商品类型名称', '创建人', '创建时间', '修改人', '修改时间'];
      this.showCol = this.columnValues;
      this.showPopover = false;
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
      }, 1000);
    },
    handleHide() {
      this.columnValues = this.showCol;
    },
    onSubmit(formName) {
      let data ={
        typeName: this.selectType.typeName,
        createDateStart: this.selectType.createDate == null || this.selectType.createDate.length == 0? null : this.formatDate(this.selectType.createDate[0]),
        createDateEnd: this.selectType.createDate == null || this.selectType.createDate.length == 0? null : this.formatDate(this.selectType.createDate[1]),
        page:this.currentPage,
        pageSize:this.pageSize
      }
      this.selectQueryList(data);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.selectType.createDate = [];
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    formatDate(dateString) {
      let date = new Date(dateString);

      let year = date.getFullYear();
      let month = ("0" + (date.getMonth() + 1)).slice(-2);
      let day = ("0" + date.getDate()).slice(-2);

      let hours = ("0" + date.getHours()).slice(-2);
      let minutes = ("0" + date.getMinutes()).slice(-2);
      let seconds = ("0" + date.getSeconds()).slice(-2);

      let formattedDateTime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;

      return formattedDateTime;
    },
    selectQueryList(selectType)
    {
      let url = "http://114.55.111.65:9090/type/query-list-page";
      //let token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mbyI6IntcInVzZXJJZFwiOjF9IiwiZXhwIjoxNjk0MTU2NzY5fQ.2l7X0-E8twF3EJt9Ek1bVFv0T7rq0UJCw8oLCZFKZV0";
      this.loading = true;
      this.axios.post(url,selectType,{headers: {
          'token': localStorage.getItem("token")
        }}).then(reponse =>{
        let code = reponse.data.statusCode;
        if (code == "200")
        {
          this.tableData = reponse.data.data.records;
          this.total = reponse.data.data.total;
        }
        else {
          this.$notify.error({
            title: '错误',
            message: reponse.data.message
          });
        }
      }).catch(error => {
        console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message
        });
      });
      setTimeout(() => {
        this.loading = false;
      }, 1000);
    },
  },
  mounted() {
    this.selectQueryList({page:this.currentPage,pageSize:this.pageSize});
  }
}
</script>

<style>

</style>